home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gsparam.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-14  |  13.1 KB  |  345 lines

  1. /* Copyright (C) 1993, 1995 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gsparam.h */
  20. /* Client interface to parameter dictionaries */
  21.  
  22. #ifndef gsparam_INCLUDED
  23. #  define gsparam_INCLUDED
  24.  
  25. /*
  26.  * Several interfaces use parameter dictionaries to communicate sets of
  27.  * (key, value) pairs from a client to an object with complex state.
  28.  * (Several of these correspond directly to similar interfaces in the
  29.  * PostScript language.) This file defines the API for parameter dictionaries.
  30.  */
  31.  
  32. #ifndef gs_param_list_DEFINED
  33. #  define gs_param_list_DEFINED
  34. typedef struct gs_param_list_s gs_param_list;
  35. #endif
  36. typedef const char *gs_param_name;
  37.  
  38. /*
  39.  * Define a structure for representing a variable-size value
  40.  * (string/name, integer array, or floating point array).
  41.  * The size is the number of elements, not the size in bytes.
  42.  * A value is persistent if it is defined as static const,
  43.  * or if it is allocated in garbage-collectable space and never freed.
  44.  */
  45.  
  46. #define _param_array_struct(sname,etype)\
  47.   struct sname { const etype *data; uint size; bool persistent; }
  48. typedef _param_array_struct(gs_param_string_s, byte) gs_param_string;
  49. typedef _param_array_struct(gs_param_int_array_s, int) gs_param_int_array;
  50. typedef _param_array_struct(gs_param_float_array_s, float) gs_param_float_array;
  51. typedef _param_array_struct(gs_param_string_array_s, gs_param_string) gs_param_string_array;
  52.  
  53. #define param_string_from_string(ps, str)\
  54.   (ps).data = (const byte *)(str), (ps).size = strlen((const char *)(ps).data),\
  55.   (ps).persistent = true
  56.  
  57. /* Define the type for dictionary and mixed-type-array values. */
  58. typedef struct gs_param_collection_s {
  59.     gs_param_list *list;
  60.     uint size;
  61. } gs_param_collection;
  62. typedef gs_param_collection gs_param_dict;
  63. typedef gs_param_collection gs_param_array;
  64.  
  65. /* Define the 'policies' for handling out-of-range parameter values. */
  66. /* This is not an enum, because some parameters may recognize other values. */
  67. #define gs_param_policy_signal_error 0
  68. #define gs_param_policy_ignore 1
  69. #define gs_param_policy_consult_user 2
  70.  
  71. /*
  72.  * Define the object procedures.  Note that the same interface is used
  73.  * both for getting and for setting parameter values.  (This is a bit
  74.  * of a hack, and we might change it someday.)  The procedures return
  75.  * as follows:
  76.  *    - 'reading' procedures ('put' operations from the client's viewpoint)
  77.  * return 1 for a missing parameter, 0 for a valid parameter, <0 on error.
  78.  *    - 'writing' procedures ('get' operations from the client's viewpoint)
  79.  * return 0 or 1 if successful, <0 on error.
  80.  */
  81.  
  82. /*
  83.  * Transmitting variable-size objects requires some extra care.
  84.  *    - When writing an array, string, name, or dictionary, the
  85.  * implementation (not the client) sets all the fields of the value.
  86.  *    - When reading an array, string, or name, the client must set
  87.  * all the fields of the value.
  88.  *    - When reading a dictionary, the client must set the size field
  89.  * before calling begin_write_dict; the implementation of begin_write_dict
  90.  * allocates the list.
  91.  */
  92.  
  93. /*
  94.  * Setting parameters must use a "two-phase commit" policy.  Specifically,
  95.  * any put_params procedure must observe the following discipline:
  96.  
  97.     1. For each parameter known to the device, ask the parameter list if
  98. there is a new value, and if so, make all necessary validity checks.  If any
  99. check fails, call param_signal_error for that parameter, but continue to
  100. check further parameters.  Normally, this step should not alter the state of
  101. the device; however, if the device allows changing any parameters that are
  102. read-only by default (for example, BitsPerPixel or ProcessColorModel), or if
  103. it replaces the default put_params behavior for any parameter (for example,
  104. if it handles MediaSize or Resolution itself to forestall the normal closing
  105. of the device when these are set), step 1 of put_params must change the
  106. parameters in the device state, and step 2 must undo the changes if
  107. returning an error.
  108.  
  109.     2. Call the "superclass" put_params routine.  For printer devices,
  110. this is gdev_prn_put_params; for other devices, it is gx_default_put_params.
  111. Note that this must be done even if errors were detected in step 1.  If this
  112. routine returns an error code, or if step 1 detected an error, undo any
  113. changes that step 1 made in the device state, and return the error code.
  114.  
  115.     3. Install the new parameter values in the device.  If necessary,
  116. close the device first; a higher-level routine (gs_putdeviceparams) will
  117. reopen the device if necessary.
  118.  
  119.  */
  120.  
  121. typedef struct gs_param_list_procs_s {
  122.  
  123.     /* Transmit a null value. */
  124.     /* Note that this is the only 'transmit' operation */
  125.     /* that does not actually pass any data. */
  126.  
  127. #define param_proc_xmit_null(proc)\
  128.   int proc(P2(gs_param_list *, gs_param_name))
  129.     param_proc_xmit_null((*xmit_null));
  130. #define param_read_null(plist, pkey)\
  131.   (*(plist)->procs->xmit_null)(plist, pkey)
  132. #define param_write_null(plist, pkey)\
  133.   (*(plist)->procs->xmit_null)(plist, pkey)
  134.  
  135.     /* Transmit a Boolean value. */
  136.  
  137. #define param_proc_xmit_bool(proc)\
  138.   int proc(P3(gs_param_list *, gs_param_name, bool *))
  139.     param_proc_xmit_bool((*xmit_bool));
  140. #define param_read_bool(plist, pkey, pvalue)\
  141.   (*(plist)->procs->xmit_bool)(plist, pkey, pvalue)
  142. #define param_write_bool(plist, pkey, pvalue)\
  143.   (*(plist)->procs->xmit_bool)(plist, pkey, pvalue)
  144.  
  145.     /* Transmit an integer value. */
  146.  
  147. #define param_proc_xmit_int(proc)\
  148.   int proc(P3(gs_param_list *, gs_param_name, int *))
  149.     param_proc_xmit_int((*xmit_int));
  150. #define param_read_int(plist, pkey, pvalue)\
  151.   (*(plist)->procs->xmit_int)(plist, pkey, pvalue)
  152. #define param_write_int(plist, pkey, pvalue)\
  153.   (*(plist)->procs->xmit_int)(plist, pkey, pvalue)
  154.  
  155.     /* Transmit a long value. */
  156.  
  157. #define param_proc_xmit_long(proc)\
  158.   int proc(P3(gs_param_list *, gs_param_name, long *))
  159.     param_proc_xmit_long((*xmit_long));
  160. #define param_read_long(plist, pkey, pvalue)\
  161.   (*(plist)->procs->xmit_long)(plist, pkey, pvalue)
  162. #define param_write_long(plist, pkey, pvalue)\
  163.   (*(plist)->procs->xmit_long)(plist, pkey, pvalue)
  164.  
  165.     /* Transmit a float value. */
  166.  
  167. #define param_proc_xmit_float(proc)\
  168.   int proc(P3(gs_param_list *, gs_param_name, float *))
  169.     param_proc_xmit_float((*xmit_float));
  170. #define param_read_float(plist, pkey, pvalue)\
  171.   (*(plist)->procs->xmit_float)(plist, pkey, pvalue)
  172. #define param_write_float(plist, pkey, pvalue)\
  173.   (*(plist)->procs->xmit_float)(plist, pkey, pvalue)
  174.  
  175.     /* Transmit a string value. */
  176.  
  177. #define param_proc_xmit_string(proc)\
  178.   int proc(P3(gs_param_list *, gs_param_name, gs_param_string *))
  179.     param_proc_xmit_string((*xmit_string));
  180. #define param_read_string(plist, pkey, pvalue)\
  181.   (*(plist)->procs->xmit_string)(plist, pkey, pvalue)
  182. #define param_write_string(plist, pkey, pvalue)\
  183.   (*(plist)->procs->xmit_string)(plist, pkey, pvalue)
  184.  
  185.     /* Transmit a name value. */
  186.  
  187. #define param_proc_xmit_name(proc)\
  188.   int proc(P3(gs_param_list *, gs_param_name, gs_param_string *))
  189.     param_proc_xmit_name((*xmit_name));
  190. #define param_read_name(plist, pkey, pvalue)\
  191.   (*(plist)->procs->xmit_name)(plist, pkey, pvalue)
  192. #define param_write_name(plist, pkey, pvalue)\
  193.   (*(plist)->procs->xmit_name)(plist, pkey, pvalue)
  194.  
  195.     /* Transmit an integer array value. */
  196.  
  197. #define param_proc_xmit_int_array(proc)\
  198.   int proc(P3(gs_param_list *, gs_param_name, gs_param_int_array *))
  199.     param_proc_xmit_int_array((*xmit_int_array));
  200. #define param_read_int_array(plist, pkey, pvalue)\
  201.   (*(plist)->procs->xmit_int_array)(plist, pkey, pvalue)
  202. #define param_write_int_array(plist, pkey, pvalue)\
  203.   (*(plist)->procs->xmit_int_array)(plist, pkey, pvalue)
  204.  
  205.     /* Transmit a float array value. */
  206.  
  207. #define param_proc_xmit_float_array(proc)\
  208.   int proc(P3(gs_param_list *, gs_param_name, gs_param_float_array *))
  209.     param_proc_xmit_float_array((*xmit_float_array));
  210. #define param_read_float_array(plist, pkey, pvalue)\
  211.   (*(plist)->procs->xmit_float_array)(plist, pkey, pvalue)
  212. #define param_write_float_array(plist, pkey, pvalue)\
  213.   (*(plist)->procs->xmit_float_array)(plist, pkey, pvalue)
  214.  
  215.     /* Transmit a string array value. */
  216.  
  217. #define param_proc_xmit_string_array(proc)\
  218.   int proc(P3(gs_param_list *, gs_param_name, gs_param_string_array *))
  219.     param_proc_xmit_string_array((*xmit_string_array));
  220. #define param_read_string_array(plist, pkey, pvalue)\
  221.   (*(plist)->procs->xmit_string_array)(plist, pkey, pvalue)
  222. #define param_write_string_array(plist, pkey, pvalue)\
  223.   (*(plist)->procs->xmit_string_array)(plist, pkey, pvalue)
  224.  
  225.     /* Transmit a name array value. */
  226.  
  227. #define param_proc_xmit_name_array(proc)\
  228.   int proc(P3(gs_param_list *, gs_param_name, gs_param_string_array *))
  229.     param_proc_xmit_name_array((*xmit_name_array));
  230. #define param_read_name_array(plist, pkey, pvalue)\
  231.   (*(plist)->procs->xmit_name_array)(plist, pkey, pvalue)
  232. #define param_write_name_array(plist, pkey, pvalue)\
  233.   (*(plist)->procs->xmit_name_array)(plist, pkey, pvalue)
  234.  
  235.     /* Start transmitting a dictionary value. */
  236.     /* If int_keys is true, the keys are actually integers */
  237.     /* (although still presented as strings). */
  238.  
  239. #define param_proc_begin_xmit_dict(proc)\
  240.   int proc(P4(gs_param_list *, gs_param_name, gs_param_dict *, bool))
  241.     param_proc_begin_xmit_dict((*begin_xmit_dict));
  242. #define param_begin_read_dict(plist, pkey, pvalue, int_keys)\
  243.   (*(plist)->procs->begin_xmit_dict)(plist, pkey, pvalue, int_keys)
  244. #define param_begin_write_dict(plist, pkey, pvalue, int_keys)\
  245.   (*(plist)->procs->begin_xmit_dict)(plist, pkey, pvalue, int_keys)
  246.  
  247.     /* Finish transmitting a dictionary value. */
  248.  
  249. #define param_proc_end_xmit_dict(proc)\
  250.   int proc(P3(gs_param_list *, gs_param_name, gs_param_dict *))
  251.     param_proc_end_xmit_dict((*end_xmit_dict));
  252. #define param_end_read_dict(plist, pkey, pvalue)\
  253.   (*(plist)->procs->end_xmit_dict)(plist, pkey, pvalue)
  254. #define param_end_write_dict(plist, pkey, pvalue)\
  255.   (*(plist)->procs->end_xmit_dict)(plist, pkey, pvalue)
  256.  
  257.     /* Determine whether a given key has been requested. */
  258.     /* (Only used when writing.) */
  259.  
  260. #define param_proc_requested(proc)\
  261.   bool proc(P2(const gs_param_list *, gs_param_name))
  262.     param_proc_requested((*requested));
  263. #define param_requested(plist, pkey)\
  264.   (*(plist)->procs->requested)(plist, pkey)
  265.  
  266.     /* Get the 'policy' associated with an out-of-range parameter value. */
  267.     /* (Only used when reading.) */
  268.  
  269. #define param_proc_get_policy(proc)\
  270.   int proc(P2(gs_param_list *, gs_param_name))
  271.     param_proc_get_policy((*get_policy));
  272. #define param_get_policy(plist, pkey)\
  273.   (*(plist)->procs->get_policy)(plist, pkey)
  274.  
  275.     /*
  276.      * Signal an error.  (Only used when reading.)
  277.      * The procedure may return a different error code,
  278.      * or may return 0 indicating that the error is to be ignored.
  279.      */
  280.  
  281. #define param_proc_signal_error(proc)\
  282.   int proc(P3(gs_param_list *, gs_param_name, int))
  283.     param_proc_signal_error((*signal_error));
  284. #define param_signal_error(plist, pkey, code)\
  285.   (*(plist)->procs->signal_error)(plist, pkey, code)
  286. #define param_return_error(plist, pkey, code)\
  287.   return_error(param_signal_error(plist, pkey, code))
  288.  
  289.     /*
  290.      * "Commit" a set of changes.  (Only used when reading.)
  291.      * This is called at the end of the first phase.
  292.      */
  293.  
  294. #define param_proc_commit(proc)\
  295.   int proc(P1(gs_param_list *))
  296.     param_proc_commit((*commit));
  297. #define param_commit(plist)\
  298.   (*(plist)->procs->commit)(plist)
  299.  
  300. } gs_param_list_procs;
  301.  
  302. /* Define an abstract parameter dictionary.  Implementations are */
  303. /* concrete subclasses. */
  304. #define gs_param_list_common\
  305.     const gs_param_list_procs _ds *procs
  306. struct gs_param_list_s {
  307.     gs_param_list_common;
  308. };
  309.  
  310. /*
  311.  * Define a default implementation, intended to be usable easily
  312.  * from C code.  The intended usage pattern is:
  313.     gs_c_param_list list;
  314.     [... other code here ...]
  315.     gs_c_param_list_write(&list, mem);
  316.     [As many as needed:]
  317.         code = param_write_XXX(&list, "ParamName", ¶m_value);
  318.         [Check code for <0]
  319.     gs_c_param_list_read(&list);
  320.     code = gs_putdeviceparams(dev, &list);
  321.     gs_c_param_list_release(&list);
  322.     [Check code for <0]
  323.     if ( code == 1 )
  324.     { code = (*dev_proc(dev, open_device))(dev);
  325.       [Check code for <0]
  326.     }
  327.  */
  328.  
  329. typedef struct gs_c_param_s gs_c_param;        /* opaque here */
  330. typedef struct gs_c_param_list_s {
  331.     gs_param_list_common;
  332.     gs_memory_t *memory;
  333.     gs_c_param *head;
  334.     uint count;
  335. } gs_c_param_list;
  336. #define private_st_c_param_list()    /* in gsparam.c */\
  337.   gs_private_st_ptrs1(st_c_param_list, gs_c_param_list, "c_param_list",\
  338.     c_param_list_enum_ptrs, c_param_list_reloc_ptrs, head)
  339. /* Clients normally allocate the gs_c_param_list on the stack. */
  340. void gs_c_param_list_write(P2(gs_c_param_list *, gs_memory_t *));
  341. void gs_c_param_list_read(P1(gs_c_param_list *));    /* switch to reading */
  342. void gs_c_param_list_release(P1(gs_c_param_list *));
  343.  
  344. #endif                    /* gsparam_INCLUDED */
  345.